﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Presenter.SolicitudesPrestamo;
using System.Collections.Generic;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class PrestamosByUser : UserControl, ISolicitudesPrestamoView
    {

        #region fields

        SolicitudesPrestamoPresenter _presenter;

        #endregion

        #region Properties

        public Guid GuidCurrentList
        {
            get { return new Guid(Page.Request.QueryString["lstID"]); }
        }
        public List<SolicitudDePrestamosContentType> listaSolicitud { get; set; }
        public List<SolicitudDePrestamosContentType> listaSolicitudPrestados { get; set; }
        public bool resultEliminarSolicitud { get; set; }
        public int idSolicitud { get; set; }

        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public int TotalRecords { get; set; }

        #endregion

        #region Form Methods

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new SolicitudesPrestamoPresenter(this);
            _presenter.CargarPrestamos();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gvSolicitudes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gvSolicitudes_RowCreated(object sender, GridViewRowEventArgs e)
        {
        }

        protected void gvSolicitudes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Controls[0].Visible = false;
            }
        }

        protected void devolverPrestamoCilck(object sender, EventArgs e)
        {
            ImageButton devolverButton = (ImageButton)sender;
            TableCell tableCell = (TableCell)devolverButton.Parent;
            GridViewRow row = (GridViewRow)tableCell.Parent;

            idSolicitud = int.Parse(row.Cells[0].Text);
            _presenter.DevolverPrestamo();

        }

        public void DesplegarVentanaModal(string Url, string Titulo, Int32 ancho, Int32 alto, Page pagina)
        {
            StringBuilder scriptModal = new StringBuilder();
            scriptModal.Append("var showMyModalDialog = function() { var options = { ");
            scriptModal.Append("url : '");
            scriptModal.Append(Url);
            scriptModal.Append("', ");
            scriptModal.Append("title: '");
            scriptModal.Append(Titulo);
            scriptModal.Append("', width: ");
            scriptModal.Append(ancho.ToString());
            scriptModal.Append(", height: ");
            scriptModal.Append(alto.ToString());
            scriptModal.Append(", dialogReturnValueCallback: onDialogClose }; SP.UI.ModalDialog.commonModalDialogOpen(" + "'" + Url + "'" + ",options, onDialogClose, null); }; ");
            scriptModal.Append("function onDialogClose(dialogResult, returnValue) { ");
            scriptModal.Append("if (dialogResult == SP.UI.DialogResult.OK) { ");
            scriptModal.Append(" SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); } ");
            scriptModal.Append(" } ");

            pagina.ClientScript.RegisterClientScriptBlock(pagina.GetType(), "Mensaje", scriptModal.ToString(), true);
            pagina.ClientScript.RegisterStartupScript(typeof(Page), "Precarga", "ExecuteOrDelayUntilScriptLoaded(showMyModalDialog, 'sp.ui.dialog.js');", true);
        }

        public void loadGrilla()
        {
            _presenter.CargarPrestamos();
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            gvSolicitudes.DataSource = listaSolicitud;
            gvSolicitudes.DataBind();

            GvPrestados.DataSource = listaSolicitudPrestados;
            GvPrestados.DataBind();

            LoadControl();
        }

        private void LoadControl()
        {
            PrestamosDocumentales pd = (PrestamosDocumentales)this.Parent.Parent;
            if (pd != null)
            {
                pd.SetPrestamos(listaSolicitud.Count + listaSolicitudPrestados.Count);
            }
        }

        public void resultCancelarSolicitud()
        {
            _presenter.CargarPrestamos();
        }

        #endregion

        protected void GvPrestados_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void GvPrestados_RowCreated(object sender, GridViewRowEventArgs e)
        {

        }

        protected void GvPrestados_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Controls[0].Visible = false;
            }
        }
    }
}